package com.lai.shop.interceptor;

import cn.hutool.core.util.ObjectUtil;
import com.lai.shop.constant.CommonConstant;
import com.lai.shop.entity.UserEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @BelongsPackage: com.lai.shop.interceptor
 * @Author: laiciqi
 * @CreateTime: 2022-05-24  10:36
 * @Description: 登录拦截器
 * @Version: 1.0
 */
@Component
public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //判断用户是否登录
        Object user = request.getSession().getAttribute(CommonConstant.LOGIN_USER);
        if(user instanceof UserEntity && ObjectUtil.isNotNull(user)){
            //登录后则不能再进入登录页面
            return false;
        }else{
            //response.sendRedirect("http://loaclhost:8099/login.html");
            return true;
        }

    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
